package cn.hnu.qq.client.panel;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.apache.log4j.Logger;

import cn.hnu.qq.client.controller.DefaultModelChartController;
import cn.hnu.qq.client.event.MessageEvent;
import cn.hnu.qq.client.listener.MessageListener;
import cn.hnu.qq.entity.User;
import cn.hnu.qq.entity.message.ChartMessage;
import cn.hnu.qq.entity.message.Message;
import cn.hnu.qq.global.MessageType;

public class DefaultModelChartPanel extends JPanel implements ActionListener,MessageListener{

	JTextArea readAble;
	JTextArea writeable;
	
	private User ownner;
	
	private DefaultModelChartController controller = DefaultModelChartController.getInstance();
	/**
	 * Create the panel.
	 */
	public DefaultModelChartPanel() {
		setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 10, 528, 267);
		add(scrollPane);
		
		readAble = new JTextArea();
		readAble.setEditable(false);//
		readAble.setLineWrap(true);
		scrollPane.setViewportView(readAble);
		
		readAble.setCaretPosition(readAble.getDocument().getLength());
	
		JPanel panel = new JPanel();
		panel.setBounds(10, 287, 528, 175);
		add(panel);
		panel.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_1 = new JPanel();
		panel.add(panel_1, BorderLayout.NORTH);
		
		JPanel panel_2 = new JPanel();
		panel.add(panel_2, BorderLayout.SOUTH);
		
		JButton sendBtn = new JButton("send");
		panel_2.add(sendBtn);
		sendBtn.addActionListener(this);
		
		JButton concelBtn = new JButton("concel");
		panel_2.add(concelBtn);
		concelBtn.addActionListener(this);
		
		writeable = new JTextArea();
		panel.add(writeable, BorderLayout.CENTER);
	
		this.setSize(554, 472);
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("send")){
			String info = writeable.getText();
			controller.sendChartMessage(ownner,info);
			writeable.setText("");
			info = "【我】" + new SimpleDateFormat("HH-mm-ss").format(new Date())+ "\r\n" + info +"\r\n";
//			readAble.insert(info, readAble.getCaretPosition());
			readAble.append(info);
			readAble.setCaretPosition(readAble.getText().length());  
		}
	}
	
	
	
	public User getOwnner() {
		return ownner;
	}
	public void setOwnner(User ownner) {
		this.ownner = ownner;
	}

	public void insertMessage(ChartMessage cm){
		String info = cm.getMes();
		info = "【" + cm.getAccount() +"】" + new SimpleDateFormat("HH-mm-ss").format(new Date())+ "\r\n" + info +"\r\n"; 
		readAble.append(info);
		readAble.setCaretPosition(readAble.getText().length());      
	}
	
	public void messageArrived(MessageEvent event) {
		Message ms = event.getSource();
		Logger.getLogger(DefaultModelChartController.class).debug("获取消息  ："+event);
		if(ms.getMessageType() == MessageType.CHART){
			String info = ((ChartMessage) ms).getMes();
			info = "【" + ((ChartMessage) ms).getAccount() +"】" + new SimpleDateFormat("HH-mm-ss").format(new Date())+ "\r\n" + info + "\r\n";
			readAble.append(info);
			readAble.setCaretPosition(readAble.getText().length());  
		}
	}
	
	
}
